<?php

class IndexController extends Controller
{
    public $layout='//layouts/body';
    public $pageTitle = '指尖上的阅读';

    public function actionChaxun(){
        header("Content-Type: text/html; charset=UTF-8");
        $db = Yii::app()->db->createCommand();
        $db->select('p.id,p.cid,c.title');
        $db->from('{{people}} p');
        $db->leftJoin('{{classes}} c','p.cid=c.id');
        $db->andWhere('p.role=:role',
            array(':role'=>1));
        $info = $db->queryAll();
        $yuedu = array();
        $pinglun = array();
        foreach($info as $v){
            $db1 = Yii::app()->db->createCommand();
            $db1->select('COUNT(*) as count');
            $db1->from('{{yuedu}}');
            $db1->andWhere('pid=:pid',
                array(':pid'=>$v['id']));
            $info1 = $db1->queryRow();

            $yuedu[$v['title']] = $yuedu[$v['title']]+$info1['count'];
        }
        echo '阅读量<br/>';
        print_r($yuedu);
        echo '学生评论<br/>';
        foreach($info as $vv){
            $db2 = Yii::app()->db->createCommand();
            $db2->select('COUNT(*) as count');
            $db2->from('{{book_comment}}');
            $db2->andWhere('pid=:pid',
                array(':pid'=>$vv['id']));
            $info2 = $db2->queryRow();
            $pinglun[$vv['title']] = $pinglun[$vv['title']]+$info2['count'];
        }
        print_r($pinglun);
        echo '老师点评<br/>';
        $db5 = Yii::app()->db->createCommand();
        $db5->select('p.id,p.cid,c.title');
        $db5->from('{{people}} p');
        $db5->leftJoin('{{classes}} c','p.cid=c.id');
        $db5->andWhere('p.role=:role',
            array(':role'=>3));
        $info5 = $db5->queryAll();
        foreach($info5 as $vvv){
            $db2 = Yii::app()->db->createCommand();
            $db2->select('COUNT(*) as count');
            $db2->from('{{book_comment}}');
            $db2->andWhere('pid=:pid',
                array(':pid'=>$vvv['id']));
            $info3 = $db2->queryRow();
            $lpinglun[$vvv['title']] = $pinglun[$vvv['title']]+$info3['count'];
        }
        print_r($lpinglun);





    }

    public function actionIndex()
    {
        //判断类型
        $app_info = CityApp::model()->findByPk(1);
        if($app_info->is_type==1){//web类型
                $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
                $this->render('index',array('openid'=>$openid,'app_info'=>$app_info,'type'=>1));
        }elseif($app_info->is_type==2){//订阅号
            $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
            if($openid){
                $user_info = People::model()->find('openid=:openid and is_login=:is_login',array(':openid'=>$openid,':is_login'=>1));
                if($user_info){//跳转
                    $this->redirect($this->createUrl('list',array('openid'=>$openid)));
                }else{
                    $this->render('index',array('app_info'=>$app_info,'type'=>2,'openid'=>$openid));
                }
            }else{
                $this->render('index',array('app_info'=>$app_info,'type'=>1));
            }
        }elseif($app_info->is_type==3){//服务号
            $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
            if($openid){
                $user_info = People::model()->find('openid=:openid and is_login=:is_login',array(':openid'=>$openid,':is_login'=>1));
                if($user_info){//跳转
                    Yii::app()->session['user_id'] = $user_info->id;
                    if($user_info->role==1){
                        $this->redirect($this->createUrl('list',array('openid'=>$openid)));
                    }else{
                        $this->redirect($this->createUrl('list1',array('openid'=>$openid)));
                    }

                }else{
                    $this->render('index',array('app_info'=>$app_info,'type'=>3));
                }
            }else{
                exit('get openid error');
            }
        }
    }
    public function actionList(){
        $openid = Yii::app()->request->getParam('openid');
        $is_recommend = Yii::app()->request->getParam('is_recommend');
        $is_book =  Yii::app()->request->getParam('is_book');
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->find('openid=:openid and is_login=:is_login',array(':openid'=>$openid,':is_login'=>1));
        $list = Book::model()->getInfo($first,$total,$is_book,$is_recommend,$user_info->gid);
        $this->render('list',array('openid'=>$openid,'list'=>$list,'is_recommend'=>$is_recommend,'is_book'=>$is_book));
    }
    public function actionList1(){
        $openid = Yii::app()->request->getParam('openid');
        $is_recommend = Yii::app()->request->getParam('is_recommend');
        $is_book =  Yii::app()->request->getParam('is_book');
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }

        $user_info = People::model()->findByPk($user_id);
        $list = Book::model()->getInfo($first,$total,$is_book,$is_recommend,$user_info->gid);


        $this->render('list1',array('user_info'=>$user_info,'openid'=>$openid,'list'=>$list,'is_recommend'=>$is_recommend,'is_book'=>$is_book));
    }
    public function actionGuide(){
        $openid = Yii::app()->request->getParam('openid');
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getGuideInfo($first,$total);
        $this->pageTitle = '读书列表';
        $this->render('guide_list',array('openid'=>$openid,'list'=>$list));
    }
    public function actionNews(){
        $openid = Yii::app()->request->getParam('openid');
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = News::model()->getGuideInfo($first,$total);
        $this->pageTitle = '阅读指导';
        $this->render('news_list',array('openid'=>$openid,'list'=>$list));
    }
    public function actionAjaxList(){
        $this->layout = '//layouts/null';
        $openid = Yii::app()->request->getParam('openid');
        $user_info = People::model()->find('openid=:openid and is_login=:is_login',array(':openid'=>$openid,':is_login'=>1));
        $is_recommend = Yii::app()->request->getParam('is_recommend');
        $is_book =  Yii::app()->request->getParam('is_book');
        $page =  Yii::app()->request->getParam('page');
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getInfo($first,$total,$is_book,$is_recommend,$user_info->gid);
        $this->renderPartial('ajax_list',array('openid'=>$openid,'list'=>$list,'is_recommend'=>$is_recommend,'is_book'=>$is_book));
    }
    public function actionAjaxGuideList(){
        $this->layout = '//layouts/null';
        $openid = Yii::app()->request->getParam('openid');
        $page =  Yii::app()->request->getParam('page');
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getGuideInfo($first,$total);
        $this->renderPartial('ajax_guide_list',array('openid'=>$openid,'list'=>$list));
    }
    public function actionAjaxNewsList(){
        $this->layout = '//layouts/null';
        $openid = Yii::app()->request->getParam('openid');
        $page =  Yii::app()->request->getParam('page');
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $list = News::model()->getGuideInfo($first,$total);
        $this->renderPartial('ajax_news_list',array('openid'=>$openid,'list'=>$list));
    }

    public function actionAjaxCheckUser(){
        if($_POST['stroeUser']){
            $id = Xtutuquan::encryption($_POST['stroeUser'],1);
            if($id){
                $user_info = People::model()->findByPk($id);
                if($user_info){
                    Yii::app()->session['user_id']=$id;
                    $data['role'] = $user_info->role;
                    $data['status'] = 1;
                }else{
                    $data['status'] = 0;
                }
            }else{
                $data['status'] = 0;
            }
            echo json_encode($data);
            exit();
        }else{
            $data['status'] = 0;
            echo json_encode($data);
            exit();
        }
    }
    //注册
    public function actionRegister(){
        $app_info = CityApp::model()->findByPk(1);
        $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
        $type = Yii::app()->request->getParam('type');
        if(!$type){
            $this->render('registers',array('openid'=>$openid));
        }else{
            if($app_info->is_type==1){//web类型
                $key =1;
            }elseif($app_info->is_type==2){//订阅号
                $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
                if($openid){
                    $user_info = People::model()->find('openid=:openid and is_login=:is_login',array(':openid'=>$openid,':is_login'=>1));
                    $key =2;
                }else{
                    $key =1;
                }
            }elseif($app_info->is_type==3){//服务号
                $user = XUtils::isAuth($app_info->wx_app_id,$app_info->wx_app_secret);
                $openid = $user['openid'];
                if($openid){
                    $user_info = People::model()->find('openid=:openid and is_login=:is_login',array(':openid'=>$openid,':is_login'=>1));
                    $key =3;
                }else{
                    exit('get openid error');
                }
            }
            if($type==2){//家长
                $this->render('register_2',array('openid'=>$openid,'key'=>$key,'user'=>$user));
            }elseif($type==3){//教师
                $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
                $this->render('register_3',array('grade'=>$grade_array,'openid'=>$openid,'key'=>$key,'user'=>$user));
            }else{//学生
                $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
                $this->render('register_1',array('grade'=>$grade_array,'openid'=>$openid,'key'=>$key,'user'=>$user));
            }
        }
    }
    public function actionAjaxLogout(){
        $id = Yii::app()->session['user_id'];
        $user_info = People::model()->findByPk($id);
        People::model()->updateAll(array('is_login'=>''),'openid=:openid',array('openid'=>$user_info->openid));
        Yii::app()->session['user_id'] = '';
        $data['status'] = 1;
        $data['message'] = '退出成功!';
        $data['url'] = $this->createUrl('index/index',array('openid'=>$user_info->openid));
        echo json_encode($data);
        exit();
    }
    public function actionAjaxSwitch(){
        $id = Yii::app()->session['user_id'];
        $openid = Yii::app()->request->getParam('openid');

        if(!$id){
            $data['status'] = 2;
            $data['message'] = 'error session';
            echo json_encode($data);
            exit();
        }
        $user_info = People::model()->findByPk($id);
        if($user_info->role==2){//切换成学生
            $child_id = $user_info->relation_id;
            $child_info = People::model()->findByPk($child_id);//孩子的信息
            //孩子登录
            People::model()->updateAll(array('is_login'=>''),'openid=:openid',array('openid'=>$openid));
            $child_info->is_login = 1;
            $child_info->update();
            $id = $child_info->id;
            $data['status'] = 1;
            $data['key'] = Xtutuquan::encryption($id);
            $data['message'] = '切换成功';
            Yii::app()->session['user_id']=$id;
            echo json_encode($data);
            exit();
        }else{
            //家长登录
            $parent_info =  People::model()->find('relation_id=:relation_id',array(':relation_id'=>$user_info->id));//家长的信息
            if(!$parent_info){
                $data['status'] = 3;
                $data['message'] = '还没有绑定家长账号';
                echo json_encode($data);
                exit();
            }
            People::model()->updateAll(array('is_login'=>''),'openid=:openid',array('openid'=>$openid));
            $parent_info->is_login = 1;
            $parent_info->update();
            $id = $parent_info->id;
            $data['status'] = 1;
            $data['key'] = Xtutuquan::encryption($id);
            $data['message'] = '切换成功';
            Yii::app()->session['user_id']=$id;
            echo json_encode($data);
            exit();
        }
    }

    public function actionAjaxRegister(){
        if(Yii::app()->request->isAjaxRequest){
            $user_name = Yii::app()->request->getParam('user_name');
            $passwd = Yii::app()->request->getParam('passwd');
            $code_num = Yii::app()->request->getParam('code_num');
            $cusername = Yii::app()->request->getParam('cusername');
            $image = Yii::app()->request->getParam('image');
            $lx = Yii::app()->request->getParam('lx');
            if($lx==1){
                $people_info = People::model()->find('code_num=:code_num',array(':code_num'=>$code_num));
                if($people_info){
                    $data['status'] = 0;
                    $data['message'] = '此学籍号已被注册!';
                    echo json_encode($data);
                    exit();
                }
                //教师库匹配
                $student_info = Student::model()->find('user_name=:user_name and code=:code',array(':user_name'=>$user_name,':code'=>$code_num));
                if(!$student_info){
                    $data['status'] = 0;
                    $data['message'] = '不存在此学生名称&学籍号信息';
                    echo json_encode($data);
                    exit();
                }
                $role = 1;

            }elseif($lx==2){//家长
                $people_info = People::model()->find('user_name=:user_name and code_num=:code_num',array(':user_name'=>$cusername,':code_num'=>$code_num));
                if(!$people_info){
                    $data['status'] = 0;
                    $data['message'] = '不存在此学生，请核对后输入学生姓名与学籍号';
                    echo json_encode($data);
                    exit();
                }
                $role = 2;
            }else{
                $people_info = People::model()->find('code_num=:code_num',array(':code_num'=>$code_num));
                if($people_info){
                    $data['status'] = 0;
                    $data['message'] = '此师训号已被注册!';
                    echo json_encode($data);
                    exit();
                }
                //教师库匹配
                $teacher_info = Teacher::model()->find('user_name=:user_name and code=:code',array(':user_name'=>$user_name,':code'=>$code_num));
                if(!$teacher_info){
                    $data['status'] = 0;
                    $data['message'] = '不存在此教师名称&师训号信息';
                    echo json_encode($data);
                    exit();
                }
                $role = 3;
            }
            $User = new People();
            $User->user_name = CHtml::encode($user_name);
            $User->is_login = 1;
            $User->passwd = sha1($passwd);
            $User->role = $role;
            $User->key_sn = $this->randStr(10);
            if($image){
                $User->image = $image;
            }
            $openid = Yii::app()->request->getParam('openid');
            if($openid){
                $User->openid = $openid;
                People::model()->updateAll(array('is_login'=>''),'openid=:openid',array('openid'=>$openid));
            }
            $user_phone = Yii::app()->request->getParam('user_phone');
            if($user_phone){
                $User->user_phone = $user_phone;
            }
            if($code_num){
                $User->code_num = $code_num;
            }

            $sex = Yii::app()->request->getParam('sex');
            if($sex){
                $User->sex = $sex;
            }
            if($lx==1){
                if(Yii::app()->request->getParam('birthday')){
                    $birthday = strtotime(Yii::app()->request->getParam('birthday'));
                    $month = date('m',$birthday);
                    $day = date('d',$birthday);
                    $User->birth_month = $month;
                    $User->birth_day = $day;
                }
                $User->start_time = strtotime(Yii::app()->request->getParam('start_time'));
            }
            $gid = Yii::app()->request->getParam('gid');
            if($gid){
                $User->gid = intval($gid);
            }
            $cid = Yii::app()->request->getParam('cid');
            if($cid){
                $User->cid = intval($cid);
            }
            if($lx==2){
                $relation_id = $people_info->id;
                $User->relation_id = $relation_id;
                $is_relation = Yii::app()->request->getParam('is_relation');
                if($is_relation){
                    $User->is_relation = $is_relation;
                }
            }
            if($lx==3){
                $is_subject = Yii::app()->request->getParam('is_subject');
                if($is_subject){
                    $User->is_subject = $is_subject;
                }
            }
            if($User->save()){
                $id = $User->attributes['id'];
                $data['status'] = 1;
                $data['key'] = Xtutuquan::encryption($id);
                $data['message'] = '注册成功';
                Yii::app()->session['user_id']=$id;
            }else{
                $data['status'] = 0;
                $data['message'] = '注册失败';
            }
            echo json_encode($data);
            exit();
        }
    }
    public function actionClickCount(){
        $id = Yii::app()->request->getParam('id');
        $info = Book::model()->findByPk($id);
        $info->click = intval($info->click)+1;
        $info->update();
    }
    public function randStr($length=10){
        $hash='';
        $chars= '0123456789';   //@#!~?:-
        $max=strlen($chars)-1;
        mt_srand((double)microtime()*1000000);
        for($i=0;$i<$length;$i++){
            $hash.=$chars[mt_rand(0,$max)];
        }
        return $hash;
    }
    public function actionLoadClass(){
        $gid = Yii::app()->request->getParam('gid');
        $class = Classes::model()->findAll('gid=:gid and status=:status and is_show=:is_show',array(':gid'=>$gid,':status'=>1,':is_show'=>1));
        $str = '';
        if($class){
            foreach($class as $v){
                $str .= '<option value="'.$v->id.'">'.$v->title.'</option>';
            }
        }
        echo $str;
    }
    //登录
    //注册
    public function actionLogin(){
        $app_info = CityApp::model()->findByPk(1);
        $user = '';
        $openid = CHtml::encode(Yii::app()->request->getParam('openid'));

        $type = Yii::app()->request->getParam('type');
        if(!$type){
            $this->render('logins',array('openid'=>$openid));
        }else{
            if($app_info->is_type==1){//web类型
                $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
                $key =1;
            }elseif($app_info->is_type==2){//订阅号
                $openid = CHtml::encode(Yii::app()->request->getParam('openid'));
                if($openid){
                    $key =2;
                }else{
                    $key =1;
                }
            }elseif($app_info->is_type==3){//服务号
                $user = XUtils::isAuth($app_info->wx_app_id,$app_info->wx_app_secret);
                $openid = $user['openid'];
                if($openid){
                    $user_info = People::model()->find('openid=:openid and is_login=:is_login',array(':openid'=>$openid,':is_login'=>1));
                    if(!$user_info){//跳转
                        $key =3;
                    }
                }else{
                    exit('get openid error');
                }
            }
            $this->render('login',array('openid'=>$openid,'key'=>$key,'user'=>$user,'type'=>$type));
        }
    }
    public function actionGetApp(){

        $url = Yii::app()->request->getParam('url');
        $app_id = Yii::app()->request->getParam('app_id');
        $app_secret = Yii::app()->request->getParam('app_secret');
        $con = Yii::app()->request->getParam('con');
        echo $result_config_data = XUtils::getSignPackage($app_id,$app_secret,$url,$con);
    }
    public function actionAjaxLogin(){
        if(Yii::app()->request->isAjaxRequest){
            $user_name = Yii::app()->request->getParam('user_name');
            $passwd = Yii::app()->request->getParam('passwd');
            $code_num = Yii::app()->request->getParam('code_num');
            $lx = Yii::app()->request->getParam('lx');//账号登陆类型
            $image = Yii::app()->request->getParam('image');
            $type = Yii::app()->request->getParam('type');//系统选择类型 1 web站 2 订阅号  3 服务号
            $openid = Yii::app()->request->getParam('openid');

            $shapass = sha1($passwd);
            $people_info = People::model()->find('role=:role and user_name=:user_name and code_num=:code_num',
                    array(':role'=>$lx,':user_name'=>$user_name,':code_num'=>$code_num));
            if(!$people_info){
                $data['status'] = 0;
                $data['message'] = '信息登录错误!';
                echo json_encode($data);
                exit();
            }
            People::model()->updateAll(array('is_login'=>''),'openid=:openid',array('openid'=>$people_info->openid));
            $people_info->is_login = 1;
            $people_info->openid = $openid;
//            if($image){
//                $people_info->image = $image;
//            }
            $people_info->update();
                $id = $people_info->id;
                $data['status'] = 1;
                $data['key'] = Xtutuquan::encryption($id);
                $data['message'] = '登录成功';
                Yii::app()->session['user_id']=$id;
            echo json_encode($data);
            exit();
        }
    }
    
    public function actionAjaxLoginProduct(){
        if(Yii::app()->request->isAjaxRequest){
            $name = Yii::app()->request->getParam('name');
            $code = Yii::app()->request->getParam('code');
            $type = Yii::app()->request->getParam('type');//系统选择类型 1 web站 2 订阅号  3 服务号
            
            $people_info = People::model()->find('user_name=:user_name and  code_num=:code_num',
                array(':user_name'=>$name,':code_num'=>$code));
            if(!$people_info){
                $data['status'] = 0;
                $data['message'] = '信息登录错误!';
                echo json_encode($data);
                exit();
            }
            People::model()->updateAll(array('is_login'=>''),'openid=:openid',array('openid'=>$people_info->openid));
            $people_info->is_login = 1;
            //            if($image){
            //                $people_info->image = $image;
            //            }
            $people_info->update();
            $id = $people_info->id;
            $data['status'] = 1;
            $data['key'] = Xtutuquan::encryption($id);
            $data['message'] = '登录成功';
            Yii::app()->session['user_id']=$id;
            echo json_encode($data);
            exit();
        }
    }
    
    public function actionShow(){
        $openid = Yii::app()->request->getParam('openid');
        $id =  Yii::app()->request->getParam('id');
        $app_info = CityApp::model()->findByPk(1);
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        $book_info = Book::model()->findByPk($id);
        if(!$book_info){
            exit("book error");
        }
        if($app_info->is_type==1){//web类型
            if($user_id){
                $user_info = People::model()->findByPk($user_id);//获取用户信息
                $status = 'ok';
            }else{//不存在
                $status = 'no';
            }
        }elseif($app_info->is_type==2){//订阅号
            if($user_id){
                $user_info = People::model()->findByPk($user_id);//获取用户信息
                $status = 'ok';
            }else{//不存在
                $user_info = People::model()->find('is_login=:is_login and openid=:openid',array(':is_login'=>1,':openid'=>$openid));//获取用户信息
                if($user_info){
                    $status = 'ok';
                }else{
                    if($openid){
                        $this->redirect($this->createUrl('index/login',array('openid'=>$openid)));
                    }else{
                        $this->redirect($this->createUrl('index/login'));
                    }
                }
            }
        }else{//服务号
            $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
            $user_info = People::model()->find('is_login=:is_login and openid=:openid',array(':is_login'=>1,':openid'=>$openid));//获取用户信息
            if(!$user_info){
                $this->redirect($this->createUrl('index/login'));
            }
        }
        $yuedu = Yuedu::model()->count('bid=:bid',array(':bid'=>$book_info->id));
        //获取评论 5条
        $comment = BookComment::model()->getInfo(0,5,$book_info->id);
        //获取用户喜欢状态
        $is_good = Xihuan::model()->find('bid=:bid and pid=:pid',array(':bid'=>$book_info->id,':pid'=>$user_info->id));
        //获取用户收藏状态
        $is_Store = Shoucang::model()->find('bid=:bid and pid=:pid',array(':bid'=>$book_info->id,':pid'=>$user_info->id));
        //获取用户阅读状态
        $time_start = strtotime(date('Ymd',time()));
        $time_end = $time_start+24*3600;
        $criteria = new CDbCriteria;
        $criteria->addCondition("bid=:bid");
        $criteria->params[':bid']=$book_info->id;
        $criteria->addCondition("pid=:pid");
        $criteria->params[':pid']=$user_info->id;
        $criteria->addCondition("create_time>:time_start");
        $criteria->params[':time_start']=$time_start;
        $criteria->addCondition("create_time<:time_end");
        $criteria->params[':time_end']=$time_end;
        $criteria->order = 'create_time DESC' ;
        $is_Yuedu = Qiandao::model()->find($criteria);
        //用户是否评论
        $is_comment = BookComment::model()->count('bid=:bid and pid=:pid',array(':bid'=>$book_info->id,':pid'=>$user_info->id));
        $this->pageTitle = '书籍详情';
        $this->render('show_1',array('is_Yuedu'=>$is_Yuedu,'is_comment'=>$is_comment,'app_info'=>$app_info,'is_Good'=>$is_good,'is_Store'=>$is_Store,'comment'=>$comment,'yuedu'=>$yuedu,'book_info'=>$book_info,'openid'=>$openid,'status'=>$status,'user_info'=>$user_info));

    }
    public function actionGuideShow(){
        $openid = Yii::app()->request->getParam('openid');
        $id =  Yii::app()->request->getParam('id');
        $app_info = CityApp::model()->findByPk(1);
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        $book_info = Book::model()->findByPk($id);
        if(!$book_info){
            exit("book error");
        }
        $this->pageTitle = $book_info->title;
        $this->render('guide_show',array('book_info'=>$book_info,'openid'=>$openid));
    }
    public function actionNewsShow(){
        $openid = Yii::app()->request->getParam('openid');
        $id =  Yii::app()->request->getParam('id');
        $app_info = CityApp::model()->findByPk(1);
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        $news_info = News::model()->findByPk($id);
        if(!$news_info){
            exit("news error");
        }
        $this->pageTitle = $news_info->title;
        $this->render('news_show',array('book_info'=>$news_info,'openid'=>$openid));
    }
    public function actionDemo(){
        $this->layout = '//layouts/null';

        $this->renderPartial('demo');
    }
    public function actionAjaxComment(){
        $this->layout = '//layouts/null';
        $page =  Yii::app()->request->getParam('page');
        $bid =  Yii::app()->request->getParam('bid');
        $pid = Yii::app()->request->getParam('pid');
        $first = $page*5;
        $total = 5;
        $comment = BookComment::model()->getInfo($first,$total,$bid);
        $user_info = People::model()->findByPk($pid);
        $this->renderPartial('ajax_comment',array('user_info'=>$user_info,'comment'=>$comment));
    }
    public function actionAjaxZan(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $cid = Yii::app()->request->getParam('cid');
        $info = Zan::model()->find('bid=:bid and cid=:cid and pid=:pid',array(':bid'=>$bid,':cid'=>$cid,':pid'=>$user_id));
        if($info){
            $data['status'] = 0;
            $data['message'] = '您已点赞';
            echo json_encode($data);
            exit();
        }else{
            $model = new Zan();
            $model->pid = $user_id;
            $model->bid = $bid;
            $model->cid = $cid;
            if($model->save()){
                $in = BookComment::model()->findByPk($cid);
                $in->good = $in->good+1;
                $in->update();
                $data['status'] = 1;
                $data['message'] = '点赞成功';

                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '点赞失败';
                echo json_encode($data);
                exit();
            }
        }
    }
    public function actionAjaxPl(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $content = CHtml::encode(Yii::app()->request->getParam('pl'));
        $parent = Yii::app()->request->getParam('cid');
        $user_info = People::model()->findByPk($user_id);
        $start = Yii::app()->request->getParam('start');
        $binfo = BookComment::model()->find('parent=:parent and bid=:bid and pid=:pid',array(':bid'=>$bid,':parent'=>$parent,':pid'=>$user_id));
        $cinfo = BookComment::model()->findByPk($parent);

        if($binfo){
            $data['status'] = 0;
            $data['message'] = '你已评论过此心得！不得重复评论';
            echo json_encode($data);
            exit();
        }else{
            //家长每日限  5次点评
            if($user_info->role==2){
                $start_time = strtotime(date('Y-m-d',time()));
                $end_time = $start_time+(24*3600);
                $jzCount = BookComment::model()->count('pid=:pid and create_time >:start_time and create_time <:end_time',array(
                   ':pid'=>$user_id,
                    ':start_time'=>$start_time,
                    ':end_time'=>$end_time
                ));
                if($jzCount >=5){
                    $data['status'] = 0;
                    $data['message'] = '家长每日只能对孩子点评五次';
                    echo json_encode($data);
                    exit();
                }
            }


            //写入数据
            //Yii::app()->params['fenxiang']
            $model = new BookComment();
            $model->pid = $user_id;
            $model->bid = $bid;
            $model->content = $content;
            $model->parent = $parent;
            $model->create_time = time();
            $model->role = $user_info->role;
            $model->good = 0;
            $model->image = '';
            $model->user_name = $user_info->user_name;
            $model->sex = $user_info->sex;
            $model->start = $start;
            $ok = $model->save();
            if($ok){
                if($user_info->role==1){
                    $this->plusPoint($user_id,'同学评价',Yii::app()->params['tongxue']);
                }elseif($user_info->role==2){
                    $this->plusPoint($cinfo->pid,'家长评价',Yii::app()->params['jiazhang']);
                }elseif($user_info->role==3){
                    $this->plusPoint($cinfo->pid,'教师评分',Yii::app()->params['laoshi']*$start);
                }
                $data['status'] = 1;
                $data['message'] = '评论成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '评论失败';
                echo json_encode($data);
                exit();
            }
        }
    }
    public function actionAjaxYuedu(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $info = Yuedu::model()->find('bid=:bid and pid=:pid',array(':bid'=>$bid,':pid'=>$user_id));
        if($info){
            $data['status'] = 2;
            $data['message'] = '您已对此书籍进行阅读';
            echo json_encode($data);
            exit();
        }else{
            $model = new Yuedu();
            $model->pid = $user_id;
            $model->bid = $bid;
            $book_info = Book::model()->findByPk($bid);
            $book_info->people = intval($book_info->people)+1;
            $book_info->update();
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '加入阅读成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '加入阅读失败';
                echo json_encode($data);
                exit();
            }
        }
    }
    public function actionAjaxShoucang(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $info = Shoucang::model()->find('bid=:bid and pid=:pid',array(':bid'=>$bid,':pid'=>$user_id));
        if($info){
            $info->delete();
            $data['status'] = 1;
            $data['message'] = '取消收藏成功';
            echo json_encode($data);
            exit();
        }else{
            $model = new Shoucang();
            $model->pid = $user_id;
            $model->bid = $bid;
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '收藏书籍成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '收藏书籍失败';
                echo json_encode($data);
                exit();
            }
        }
    }
    public function actionAjaxXihuan(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $info = Xihuan::model()->find('bid=:bid and pid=:pid',array(':bid'=>$bid,':pid'=>$user_id));
        if($info){
            $info->delete();
            $data['status'] = 1;
            $data['message'] = '取消喜欢成功';
            echo json_encode($data);
            exit();
        }else{
            $model = new Xihuan();
            $model->pid = $user_id;
            $model->bid = $bid;
            $book_info = Book::model()->findByPk($bid);
            $book_info->good = intval($book_info->good)+1;
            $book_info->update();
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '加入喜欢成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '加入喜欢失败';
                echo json_encode($data);
                exit();
            }
        }
    }
    public function plusPoint($user_id,$message,$point){
        //先去查看用户积分状况   统计积分数
        $jiaDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=1 AND pid=$user_id");
        $jia = $jiaDb->queryRow();
        $jianDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=0 AND pid=$user_id");
        $jian = $jianDb->queryRow();
        $count = $jia['count'] - $jian['count'];//总数
        //插入增加数据
        $model = new Point();
        $model->create_time = time();
        $model->title = $message;
        $model->type = 1;
        $model->num = $point;
        $model->pid = $user_id;
        $model->save();
        //修改用户总积分状态
        $user_info = People::model()->findByPk($user_id);
        $user_info->point = $count+$point;
        $user_info->update();
    }
    public function actionAjaxQiandao(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $time = date('Ymd',time());

        $criteria=new CDbCriteria;
        $criteria->condition='pid=:pid';
        $criteria->params= array(':pid'=>$user_id);
        $criteria->order = 'id desc';
        //是否立即阅读
        $yuedu_info = Yuedu::model()->find('pid=:pid and bid=:bid',array(':pid'=>$user_id,':bid'=>$bid));
        if(!$yuedu_info){
            $data['status'] = 0;
            $data['message'] = '你还未立即阅读本书，不得签到。';
            echo json_encode($data);
            exit();
        }
        $info = Qiandao::model()->find($criteria);
        if($info){
            if($time==date('Ymd',$info->create_time)){
                $data['status'] = 0;
                $data['message'] = '你今天已经签过到啦';
                echo json_encode($data);
                exit();
            }else{
                $model = new Qiandao();
                $model->pid = $user_id;
                $model->bid = $bid;
                $model->create_time = time();
                if($model->save()){
                    //增加积分
                    $this->plusPoint($user_id,'读书签到',Yii::app()->params['qiandao']);
                    $data['status'] = 1;
                    $data['message'] = '签到成功';
                    echo json_encode($data);
                    exit();
                }else{
                    $data['status'] = 0;
                    $data['message'] = '签到失败';
                    echo json_encode($data);
                    exit();
                }
            }
        }else{//第一次读书签到
            $model = new Qiandao();
            $model->pid = $user_id;
            $model->bid = $bid;
            $model->create_time = time();
            if($model->save()){
                //增加积分
                $this->plusPoint($user_id,'读书签到',Yii::app()->params['qiandao']);
                $data['status'] = 1;
                $data['message'] = '签到成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '签到失败';
                echo json_encode($data);
                exit();
            }
        }
    }



    public function actionAjaxShare(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $info = Yuedu::model()->find('bid=:bid and pid=:pid',array(':bid'=>$bid,':pid'=>$user_id));
        if(!$info){
            $data['status'] = 0;
            $data['message'] = '你还没有对此书籍进行阅读签到，不能发布心得';
            echo json_encode($data);
            exit();
        }else{
            $data['status'] = 1;
            $data['message'] = 'OK';
            echo json_encode($data);
            exit();
        }
    }
    public function actionAjaxBookComment(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $bid = Yii::app()->request->getParam('bid');
        $content = CHtml::encode(Yii::app()->request->getParam('xinde'));
        $binfo = BookComment::model()->find('pid=:pid and bid=:bid',array(':bid'=>$bid,':pid'=>$user_id));
        $user_info = People::model()->findByPk($user_id);
        if($binfo){
            $data['status'] = 0;
            $data['message'] = '你已经对此书籍分享过心得啦！不得重复分享';
            echo json_encode($data);
            exit();
        }else{
            $role = Yii::app()->request->getParam('role');
            if($role !=3){
                $role = 1;
            }
            //写入数据
            //Yii::app()->params['fenxiang']
            $model = new BookComment();
            $model->pid = $user_id;
            $model->bid = $bid;
            $model->content = $content;
            $model->parent = 0;
            $model->create_time = time();
            $model->role = $role;
            $model->good = 0;
            $model->image = $user_info->image;
            $model->user_name = $user_info->user_name;
            $model->sex = $user_info->sex;
            $ok = $model->save();
            if($ok){
                //获得双倍积分
                $month = intval(date('m',time()));
                $day = intval(date('d',time()));
                if($user_info->birth_month==$month && $user_info->birth_day==$day){
                    $this->plusPoint($user_id,'心得分享',Yii::app()->params['fenxiang']*2);
                }else{
                    $this->plusPoint($user_id,'心得分享',Yii::app()->params['fenxiang']);
                }

                $data['status'] = 1;
                $data['message'] = '心得分享成功';
                $str = '<li><div class="sb"><div class="top_box">';
                if(!$user_info->image){
                    if(!$user_info->image){
                        $str .= '<div class="tx"><img src="'.$this->staticsPath."/img/tx1.jpg".'" /></div>';
                    }else{
                        $str .= '<div class="tx"><img src="'.$this->staticsPath."/img/tx2.jpg".'" /></div>';
                    }
                }else{
                    $str .= '<div class="tx"><img src="'.$user_info->image.'" /></div>';
                }
                $str .= '<div class="wz"><div class="name">'.$user_info->user_name.'</div><div class="time">刚刚</div></div>';
                $str .= '</div><div class="clearfix"></div></div>';
                $str .= '<div class="con">'.$content.'</div></div></li>';
                $data['html'] = $str;
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '心得发布失败';
                echo json_encode($data);
                exit();
            }
        }
    }
}